package com.wephone.Student.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.wephone.Student.R;
import com.wephone.Student.bean.EditMyInfoResponse;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.ShareReferenceUtil;

public class MySignatureActivity extends BaseActivity {
	private EditText signEdit;

	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(MySignatureActivity.this,EditMyInfoResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					EditMyInfoResponse res = (EditMyInfoResponse)response;
					BaseActivity.infoData = res.getData();
					String infoJson = gson.toJson(res.getData());
					ShareReferenceUtil.saveUserInfoJson(getApplicationContext(), infoJson);
					Toast.makeText(getApplicationContext(), R.string.my_signature_commit_success, Toast.LENGTH_SHORT).show();
					finish();
				}
			}
		};
		return handler;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_signature);
		
		init();
	}
	
	private void init(){
		signEdit = (EditText)findViewById(R.id.my_signature_input_edit);
		signEdit.setText(BaseActivity.getMyInfo(getApplicationContext()).getuSign());
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		findViewById(R.id.btn_finish).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				RequestParams params = new RequestParams();
				params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
				if(TextUtils.isEmpty(signEdit.getText().toString())){
					params.put("uSign", "暂未编辑签名");
				}else{
					params.put("uSign", signEdit.getText().toString());
				}
				HttpClient.getInstance().post(Constants.API_USER_EDIT_USER_URL, params, getHandler());
			}
		});
	}

}
